<template>
  <el-dialog
    v-model="state.dialogVisible"
    :append-to-body="true"
    :before-close="close"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    :destroy-on-close="true"
    top="5vh"
    :modal="model"
    :title="state.dialogTitle"
    :width="width"
    draggable
  >
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-button icon="remove" type="danger" @click="handleDelete"
          >批量删除</el-button
        >
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <RightToolBar :export="false" @refresh="queryTableData" />
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableRef"
      v-loading="tableOptions.loading"
      border
      :data="tableOptions.dataList"
      :cell-style="tableStyle.cellStyle"
      :header-cell-style="tableStyle.headerCellStyle"
      :height="tableOptions.height"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
      @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column type="index" label="序号" width="55" />
      <el-table-column
        prop="id"
        label="流程ID"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        prop="flowName"
        label="流程名称"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="flowKey"
        label="流程Key"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        prop="flowIcon"
        label="图标 "
        show-overflow-tooltip
        width="80"
      >
        <template #default="scope">
          <IconifyIconOnline
            :icon="scope.row.icon.name"
            :style="{
              background: scope.row.icon.bgc,
              color: scope.row.icon.color
            }"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="groupDesc"
        label="流程分组"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        prop="statusDesc"
        label="状态"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        prop="version"
        label="版本"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        prop="remark"
        label="备注"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column
        prop="createUserName"
        label="创建人"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="scope">
          <div
            style="
              display: flex;
              align-items: center;
              justify-content: space-between;
            "
          >
            <el-dropdown>
              <el-button icon="ArrowDown" text type="primary">
                功能
                <arrow-down />
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item
                    icon="view"
                    @click="handleEdit('view', scope.row.id)"
                  >
                    查看
                  </el-dropdown-item>
                  <el-dropdown-item divided icon="delete" @click="handleDelete"
                    >删除</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <el-button
              icon="edit-pen"
              text
              type="primary"
              @click="handleDesign(scope.row)"
              >设计</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="tableOptions.pagination.current"
      :layout="tableOptions.pagination.layout"
      :page-size="tableOptions.pagination.size"
      :total="tableOptions.pagination.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <form-dialog ref="formDialogRef" @refresh="queryTableData" />
    <CommonDialog
      v-model="designerVisible"
      :border="true"
      fullscreen
      :show-footer="false"
      :show-header="false"
      @closed="designerVisible = false"
    >
      <ProcessDesignerPanel
        fullscreen
        v-model:processDef="process"
        @closed="designerVisible = false"
      />
    </CommonDialog>
  </el-dialog>
</template>
<script setup name="FlowDef" lang="ts">
import { useTable } from "@/hooks/table";
import { getPage, delObjs } from "@/api/flow/def";
import { useMessage, useMessageBox } from "@/hooks/message";
import { cloneObject } from "@/utils/common";
import RightToolBar from "@/components/rightToolBar.vue";
import FormDialog from "./form.vue";
import type { BasicTableProps } from "@/hooks/table";
import ProcessDesignerPanel from "@/views/flow/designpanel/index.vue";

const props = defineProps({
  title: {
    type: String,
    default: "流程历史版本"
  },
  width: {
    type: String,
    default: "70%"
  },
  model: {
    type: Boolean,
    default: true
  }
});

// 定义变量内容
const formDialogRef = ref(null);
const tableRef = ref(null);
const designerVisible = ref(false);

const process = ref({
  id: "",
  flowName: "",
  flowKey: "",
  icon: {
    name: "file-icons:omnigraffle",
    bgc: "#4C87F3",
    color: "#FFFFFF"
  },
  formId: "",
  formName: "",
  status: "-1",
  version: 1,
  sort: 1,
  groupId: "1",
  remark: null,
  flowDef: "",
  flowDefJson: []
});

const state = reactive({
  dialogTitle: props.title,
  dialogVisible: false
});

const tableOptions = reactive<BasicTableProps>({
  queryForm: {
    flowKey: ""
  },
  autoLoad: false,
  queryFunction: getPage,
  descs: ["create_time"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange,
  downBlobFile
} = useTable(tableOptions, tableRef);

const handleEdit = (type, id) => {
  formDialogRef.value.show(type, id);
};

// 删除操作
const handleDelete = async () => {
  useMessageBox()
    .confirm("此操作将永久删除")
    .then(async () => {
      let ids = tableOptions.dataSelections
        .map(item => item.id)
        .join() as string;
      await delObjs(ids);
      queryTableData();
      useMessage().success("删除成功");
    });
};

const handleDesign = defFlow => {
  cloneObject(process.value, defFlow);
  designerVisible.value = true;
};

const show = flowKey => {
  queryTableData({ flowKey: flowKey });
  state.dialogVisible = true;
};

const close = () => {
  state.dialogTitle = props.title;
  state.dialogVisible = false;
};

defineExpose({
  show,
  close
});
</script>
